package com.ipfsemc.ipfsemc.service.imp;

import com.ipfsemc.ipfsemc.mapper.ApplyMapper;
import com.ipfsemc.ipfsemc.mapper.UserInfoMapper;
import com.ipfsemc.ipfsemc.mapper.UserSpaceMapper;
import com.ipfsemc.ipfsemc.model.*;
import com.ipfsemc.ipfsemc.service.IApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ApplyServiceImpl implements IApplyService {

    @Resource
    private ApplyMapper applyMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserSpaceMapper userSpaceMapper;

    @Override
    public int add(Apply apply) {

        List<Apply> applies = applyMapper.selectByAppli(apply.getUserId());
        for (Apply a : applies) {
            if (apply.getUserId().equals(a.getUserId())&&apply.getApplicantId().equals(a.getApplicantId())){
                return -4;
            }

            if (null != a && (a.getApplySate() == 0 || a.getApplySate() == 1)) {
                return a.getApplySate();
            }
        }

        int i = applyMapper.insertSelective(apply);
        if (i < 0) {
            i = -3;
        } else {
            i = 3;
        }
        return i;
    }

    @Override
    public Apply selectApply(Integer applyId) {
        return applyMapper.selectByPrimaryKey(applyId);
    }

    @Override
    public List<Apply> list(Apply apply) {

        return applyMapper.list(apply);
    }

    @Override
    public List<Apply> selectByUser(Apply apply) {
        return applyMapper.selectByUser(apply);
    }

    @Override
    public List<ApplyBase> selectFilCon(String applicantId) {
        return applyMapper.selectFilCon(applicantId);
    }

    @Override
    public List<ApplyBase> FirSpace(String applicantId) {
        UserSpaceVo u=null;
        List<ApplyBase> applyBases = applyMapper.FirSpace(applicantId);
        for (ApplyBase a:applyBases){
            u=new UserSpaceVo();
            u.setUserId(a.getUserId());
            List<UserSpaceVo> us = userSpaceMapper.listByUser(u);
            a.setListSpace(us);
        }
        return applyBases;
    }

    @Override
    public List<ApplySpace> FileCoinDetails(String applicantId) {
        return applyMapper.FileCoinDetails(applicantId);
    }

    @Override
    public int del(Integer applyId) {
        return applyMapper.deleteByPrimaryKey(applyId);
    }

    @Override
    public int updateState(Apply apply) {
        //审核通过并且申请人的报备数+1

        //审核通过，过期时间设置为三个月后
        if (apply.getApplySate() == 0) {


            apply.setPassTime(new Date());
        }

        return applyMapper.updateByPrimaryKey(apply);
    }

    @Override
    public Apply isApply(Apply apply) {
        return applyMapper.isApply(apply);
    }


}
